package com.example.cardgame

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class InfoActivity : AppCompatActivity() {
    private var currentHighScore: Int = 0 // 存储当前最高分

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_info)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        displayPlayerInfo()
    }

    @SuppressLint("SetTextI18n")
    private fun displayPlayerInfo() {
        val avatarImage = findViewById<ImageView>(R.id.avatar_image)
        val name = findViewById<TextView>(R.id.name_text)
        val gender = findViewById<TextView>(R.id.gender_text)
        val studentIdText = findViewById<TextView>(R.id.student_id_text)
        val phone = findViewById<TextView>(R.id.phone_text)
        val highScoreText = findViewById<TextView>(R.id.high_score_text)
        val email = findViewById<TextView>(R.id.email_text)
        val hobbies = findViewById<TextView>(R.id.hobbies_text)

        // 默认头像
        avatarImage.setImageResource(R.drawable.card)

        // 从 Intent 中获取数据
        val intent = intent
        val playerName = intent.getStringExtra("name") ?: getString(R.string.name)
        val playerGender = intent.getStringExtra("gender") ?: getString(R.string.sex)
        val studentId = intent.getStringExtra("studentId") ?: getString(R.string.num)
        val playerPhone = intent.getStringExtra("phone") ?: getString(R.string.phone)
        val playerEmail = intent.getStringExtra("email") ?: getString(R.string.address)
        val playerHobbies = intent.getStringExtra("hobbies") ?: getString(R.string.bobby)
        val newScore = intent.getIntExtra("score", 0) // 获取新的分数
        val avatarUriString = intent.getStringExtra("avatarUri")

        // 更新最高分
        updateHighScore(newScore)

        // 更新界面显示
        name.text = "姓名: $playerName"
        gender.text = "性别: $playerGender"
        studentIdText.text = "学号: $studentId"
        phone.text = "电话: $playerPhone"
        email.text = "邮箱: $playerEmail"
        hobbies.text = "爱好: $playerHobbies"
        highScoreText.text = "最高分: $currentHighScore"

        // 如果有头像 URI，设置头像
        avatarUriString?.let {
            val uri = Uri.parse(it)
            avatarImage.setImageURI(uri)
        }
    }

    // 更新最高分的方法
    private fun updateHighScore(newScore: Int) {
        if (newScore > currentHighScore) {
            currentHighScore = newScore // 更新当前最高分
        }
    }

    fun onEditInfo(view: View) {
        val intent = Intent(this, EditActivity::class.java)
        startActivity(intent)
    }

    fun onStartGame(view: View) {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
    }
}
